package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1022 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        int sum = 0;

        public int sumRootToLeaf(TreeNode root) {
            sumRootToLeafRecur(root, 0);
            return sum;
        }

        public void sumRootToLeafRecur(TreeNode root, int prevSum) {
            if (null == root) return;
            if (null == root.left && null == root.right) {
                sum += prevSum * 2 + root.val;
                return;
            }

            sumRootToLeafRecur(root.left, prevSum * 2 + root.val);
            sumRootToLeafRecur(root.right, prevSum * 2 + root.val);
        }
    }

    public void run() {
        System.out.println(new Solution().sumRootToLeaf(TreeNode.createTree("[1,0,1,0,1,0,1]")));
        System.out.println(new Solution().sumRootToLeaf(TreeNode.createTree("[1]")));
    }

    public static void main(String[] args) throws Exception {
        LC1022 an = new LC1022();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
